package com.bright.controller.index;



import com.bright.pojo.Blog;
import com.bright.pojo.Tag;
import com.bright.pojo.Type;
import com.bright.service.BlogService;
import com.bright.service.TagService;
import com.bright.service.TypeService;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/index")
public class IndexPageController {


    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;


    @GetMapping("/blogs")
    private List<Blog> indexDataBlogs(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                         Pageable pageable){

        return blogService.listBlogsPublished(8);
    }

    @GetMapping("/types")
    private List<Type> indexDataTypes(@RequestParam(required = false) Integer size){

       List<Type> types =  typeService.listTypeTop(size);

        for (Type type : types) {
            type.setBlogsize(type.getBlogs().size());
        }
        return types;
    }

    @GetMapping("/type/blog")
    private List<Blog> indexDataTypeBlog(long id){
        return typeService.listTypeBlog(id);
    }


    @GetMapping("/typeblogs")
    private List<Blog> indexDataTypeBlog(@RequestParam Integer typeid){
        Type type = typeService.getType(typeid);
        return type.getBlogs();
    }


    @GetMapping("/tags")
    private List<Tag> indexDataTags(@RequestParam(required = false) Integer size){

        List<Tag> tags =  tagService.listTagTop(size);

        for (Tag tag : tags) {
            tag.setBlogsize(tag.getBlogs().size());
        }
        return tags;
    }

    @GetMapping("/tag/blog")
    private List<Blog> indexDataTagBlog(long id){
        return tagService.listTagBlog(id);
    }

    @GetMapping("/search")
    private List<Blog> indexdatasearch(@RequestParam String query){
        return blogService.listBlogQuery(query);
    }

    @GetMapping("/udviews")
    private void updateviews(@RequestParam long id){
        blogService.updateviews(id);
    }
}
